package com.ssmeasy.jdgl.controller;

import com.ssmeasy.jdgl.pojo.Room;
import com.ssmeasy.jdgl.pojo.RoomDetail;
import com.ssmeasy.jdgl.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@RequestMapping("room")
public class RoomController {


    @Autowired
    private RoomService roomService;

    /**
     * @api {post} /room/addRoom/secure 添加房间
     * @apiName addRoom
     * @apiGroup Room
     * @apiVersion 0.1.0
     *
     * @apiDescription 添加房间
     *
     * @apiParam {int} id  房间号
     * @apiParam {String} level  级别
     * @apiParam {String} type  类型
     * @apiParam {int} price  价格
     * @apiParam {int} status  状态
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "res":true
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"res":false
     *  }
     */
    @RequestMapping("/addRoom/secure")
    @ResponseBody
    public Map<String,Boolean> addRoom(Room room){
        //返回值Map
        Map<String,Boolean> result = new HashMap<>();


        try {
            roomService.addRoom(room);
            result.put("res",true);
        }catch (Exception e){
            e.printStackTrace();
            result.put("res",false);
        }

        return result;

    }


    /**
     * @api {post} /room/getRoomsByStatus/secure 通过状态获取房间信息列表
     * @apiName getRoomsByStatus
     * @apiGroup Room
     * @apiVersion 0.1.0
     *
     * @apiDescription 通过状态获取房间信息列表
     *
     * @apiParam {int} status  房间状态（选填，如果不填获取全部信息）
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *
     *      "list":[
     *             	{
     *               	"id":201,
     *               	"level":"豪华",
     *               	"type":"标间",
     *               	"price":50,
     *               	"status":0
     *               }
     *             ]
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"list":null
     *  }
     */
    @RequestMapping("/getRoomsByStatus/secure")
    @ResponseBody
    public Map<String, List<Room>> getRoomsByStatus(@RequestParam(value="status",required=false,defaultValue="-1")int status){

        //返回对象
        Map<String,List<Room>> result = new HashMap<>();

        List<Room> list;

        if(status!=-1)
            //按照状态查询房间列表
            list = roomService.getRoomByStatus(status);
        else
            //默认查询所有房间列表
            list = roomService.getAllRoom();

        if(list!=null)
            result.put("list",list);
        else
            result.put("list",null);


        return result;

    }




    /**
     * @api {post} /room/getRoomDetail/secure 获取房间详情
     * @apiName getRoomDetail
     * @apiGroup Room
     * @apiVersion 0.1.0
     *
     * @apiDescription 获取房间详情
     *
     * @apiParam {int} id  房间id
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *
     *      "detail":{
     *               	"id":201,
     *               	"level":"豪华",
     *               	"type":"标间",
     *               	"price":50,
     *               	"status":0,
     *               	"orders":[
     *               			  	"id":1,
     *               				"roomid":201,
     *               				"name":"张三",
     *               				"idcard":"500212542112541",
     *               				"starttime":"2018-10-12 10:00:00",
     *               				"endtime":"2018-10-13 10:00:00"
     *               			 ]
     *               }
     *
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"detail":null
     *  }
     */
    @RequestMapping("/getRoomDetail/secure")
    @ResponseBody
    public Map<String, RoomDetail> getRoomDetail(int id){

        //返回对象
        Map<String,RoomDetail> result = new HashMap<>();

        //获取房间详细信息
        RoomDetail roomDetail = roomService.getRoomDetail(id);
        if(roomDetail!=null)
            result.put("detail",roomDetail);
        else
            result.put("detail",null);

        return result;

    }

    /**
     * @api {post} /room/getRoom/secure 获取房间信息
     * @apiName getRoom
     * @apiGroup Room
     * @apiVersion 0.1.0
     *
     * @apiDescription 获取房间信息
     *
     * @apiParam {int} id  房间id
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *
     *      "room":	{
     *               	"id":201,
     *               	"level":"豪华",
     *               	"type":"标间",
     *               	"price":50,
     *               	"status":0
     *               }
     *
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"room": null
     *  }
     */
    @RequestMapping("/getRoom/secure")
    @ResponseBody
    public Map<String,Room> getRoom(int id){

        //返回对象
        Map<String,Room> result = new HashMap<>();

        //获取一个房间信息
        Room room = roomService.getRoomById(id);

        if(room!=null)
            result.put("room",room);
        else
            result.put("room",null);

        return result;

    }



    /**
     * @api {post} /room/deleteRoom/secure 删除房间
     * @apiName deleteRoom
     * @apiGroup Room
     * @apiVersion 0.1.0
     *
     * @apiDescription  删除房间
     *
     * @apiParam {int} id  房间号
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "res":true
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"res":false
     *  }
     */
    @RequestMapping("/deleteRoom/secure")
    @ResponseBody
    public Map<String,Boolean> deleteRoom(int id){

        //返回值Map
        Map<String,Boolean> result = new HashMap<>();


        try {
            roomService.deleteRoom(id);
            result.put("res",true);

        }catch (Exception e){
            e.printStackTrace();
            result.put("res",false);
        }



        return result;

    }



    /**
     * @api {post} /room/updateRoom/secure 更新房间
     * @apiName updateRoom
     * @apiGroup Room
     * @apiVersion 0.1.0
     *
     * @apiDescription 更新房间
     *
     * @apiParam {int} id  房间号
     * @apiParam {String} level  级别
     * @apiParam {String} type  类型
     * @apiParam {int} price  价格
     * @apiParam {int} status  状态
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "res":true
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"res":false
     *  }
     */
    @RequestMapping("/updateRoom/secure")
    @ResponseBody
    public Map<String,Boolean> updateRoom(Room room){

        //返回值Map
        Map<String,Boolean> result = new HashMap<>();



        try {
            roomService.updateRoom(room);
            result.put("res",true);

        }catch (Exception e){
            e.printStackTrace();
            result.put("res",false);
        }

        return result;

    }







}
